package net.mypapit.mobile.BatteryStat;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.widget.RemoteViews;

public class BatteryStatWidgetProvider extends AppWidgetProvider {
	
	RemoteViews remoteview;	
	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
		
		
		
		
		ComponentName batteryStatWidget;
		
		context.getApplicationContext().registerReceiver(this,
				new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		
		
		remoteview = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
		
		Intent intent = new Intent(context, BatteryStatWidgetProvider.class);
		
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		remoteview.setTextViewText(R.id.wbatlevel, "widget update");
		
		batteryStatWidget = new ComponentName(context, BatteryStatWidgetProvider.class );
		
		
		
		appWidgetManager.updateAppWidget(batteryStatWidget, remoteview);
		
		
		
		
	}
	
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();

		if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
			//Integer level = intent.getIntExtra("level", -1);
			
			int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
 			int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
 			int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
 			int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
 			int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
 			int volt= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
 			
 			

 			int percent = (level*100)/scale;
 			
 			//pb.setProgress(percent);
 			//tvTemp.setText( temp/10+ " \u2103 / " + volt +" mV");
 			
 			StringBuffer sbstatus = new StringBuffer("");
 			StringBuffer sbhealth = new StringBuffer("");
 			
 			switch(status){
 				case BatteryManager.BATTERY_STATUS_CHARGING:
 					sbstatus.append("Charging");
 				break;
 				case BatteryManager.BATTERY_STATUS_DISCHARGING:
 					sbstatus.append("Discharging");
 				break;
 				case BatteryManager.BATTERY_STATUS_FULL:
 					sbstatus.append("Fully charged");
 				break;
 				
 				case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
 					sbstatus.append("Not Charging");
 				break;
 				
 				default:
 					sbstatus.append("Unknown status");
 				
 			
 			}
 			
 			switch (health){
 			case BatteryManager.BATTERY_HEALTH_COLD:
 				sbhealth.append("Battery is Cold");
 			break;
 			
 			case BatteryManager.BATTERY_HEALTH_GOOD:
 				sbhealth.append("Good Condition");
 			break;
 			
 			case BatteryManager.BATTERY_HEALTH_DEAD:
 				sbhealth.append("Dead Battery- replace immediately");
 			break;
 			
 			case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
 				sbhealth.append("Overvoltage - please disconnect power source");
 			break;
 			
 			case BatteryManager.BATTERY_HEALTH_UNKNOWN:
 				sbhealth.append("Unknown Condition");
 			break;
 			
 			case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
 				sbhealth.append("Fail to detect battery condition");
 			break;
 				
 			
 			}
 			
 			
 			
 			
 			
			this.remoteview.setTextViewText(R.id.wbatlevel, level + " %");
			remoteview.setTextViewText(R.id.wbathealth, sbhealth.toString());
			remoteview.setTextViewText(R.id.wbatstatus, sbstatus.toString());
			remoteview.setTextViewText(R.id.wbatvolt, temp/10+ " \u2103 / " + volt +" mV");
			
			
			
			ComponentName cn =
				new ComponentName(context, BatteryStatWidgetProvider.class);
			AppWidgetManager.getInstance(context).updateAppWidget(cn, this.remoteview);
		}
		super.onReceive(context, intent);
	}

}
